package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author ：daqiang
 * @date ：Created in 2019/11/24
 * @description ：
 * @version: 1.0
 */

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    /**
     * 步骤：
     *  1.判断是否是会员，如果是，直接返回
     *  2.如果不是，注册为会员
     * @param map
     */
    @Override
    public Member handleMember(Map<String, String> map) {
        String telephone = map.get("telephone");
        //通过手机号码判断会员是否存在
        Member member = memberDao.findByTelephone(telephone);
        if (member == null){
            //创建会员
            member = new Member();
            //手机号码
            member.setPhoneNumber(telephone);
            //设置档案编号
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String fileNumber = sdf.format(new Date()) + ValidateCodeUtils.generateValidateCode(4);

            member.setFileNumber(fileNumber);

            member.setRegTime(new Date());

            memberDao.add(member);
        }

        return member;
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }
}
